import {createRouter, createWebHistory} from "vue-router";
import Index from "../view/Index.vue";
import Home from "../view/user/home/Home.vue"
import Login from "../view/login/Login.vue";
import Registry from "../view/login/Registry.vue";
import TeacherLayout from "../view/teacher/Index.vue";
import AdminLayout from "../view/admin/Index.vue";
import {ElMessage} from "element-plus";
import BackPasswordNew from "../view/login/BackPasswordNew.vue";
import TeacherStudentIndex from "../view/teacher/student/Index.vue";
import TeacherStudentList from "../view/teacher/student/List.vue";
import Profile from "../view/user/student/Profile.vue";
import AdminStudentIndex from "../view/admin/student/Index.vue";
import AdminStudentManageList from "../view/admin/student/AdminStudentManageList.vue";
import TeacherSelfIndex from "../view/teacher/self/Index.vue";
import TeacherSelfInfoIndex from "../view/teacher/self/SelfIndex.vue";
import AdminTeacherManageList from "../view/admin/teacher/AdminTeacherManageList.vue";
import AdminTeacherIndex from "../view/admin/teacher/Index.vue";
import ResearchInfoIndex from "../view/teacher/researchInfo/Index.vue";
import ResearchInfoList from "../view/teacher/researchInfo/ResearchInfoList.vue";
import StudentResearchInfo from "../view/user/student/StudentResearchInfo.vue";
import TestEssentialsList from "../view/teacher/testEssentials/TestEssentialsList.vue";
import TestEssentialsIndex from "../view/teacher/testEssentials/Index.vue";
import StudentTestEssentialsList from "../view/user/student/StudentTestEssentialsList.vue";
import ExaminationQuestionList from "../view/teacher/examinationQuestion/ExaminationQuestionList.vue";
import ExaminationQuestionIndex from "../view/teacher/examinationQuestion/Index.vue";
import StudentExaminationQuestionList from "../view/user/student/question/StudentExaminationQuestionList.vue";
import StudentQuestionIndex from "../view/user/student/question/Index.vue";
import StudentAnswerQuestion from "../view/user/student/question/StudentAnswerQuestion.vue";
import AnswerRecordList from "../view/teacher/answerRecord/AnswerRecordList.vue";
import AnswerRecordIndex from "../view/teacher/answerRecord/AnswerRecordList.vue";
import StudentAnswerRecordList from "../view/user/student/answerRecord/StudentAnswerRecordList.vue";
import StudentAnswerRecordIndex from "../view/user/student/answerRecord/Index.vue";
import StudentErrorQuestionList from "../view/user/student/answerRecord/StudentErrorQuestionList.vue";
import AnswerData from "../view/teacher/answerData/AnswerData.vue";
import AnswerDataIndex from "../view/teacher/answerData/Index.vue";

// 修改管理员模块的组件导入路径

// 子页面组件

//meta元数据 title用于面包屑展示。
const routes = [
    {//一进来登录
        path: '/',
        redirect: '/login'
    },
    {//登录页
        path: '/login',
        name: 'login',
        component: Login,
    },
    {//注册页
        path: '/registry',
        name: 'registry',
        component: Registry,
    },
    // {//找回密码
    //     path: '/backPassword',
    //     name: 'backPassword',
    //     component: BackPassword,
    // },
    {//找回密码[账号密码找回]
        path: '/backPasswordNew',
        name: 'backPasswordNew',
        component: BackPasswordNew,
    },
    {//主体框架
        path: '/index',
        name: 'index',
        component: Index,
        meta: {title: '首页'},
        redirect: '/home', //默认组件展示，切到index组件的时候，就去显示地址为/home的组件
        children: [ //子路由 点击左侧导航栏跳转到指定子路由
            {//首页
                path: '/home',
                component: Home,
                name: 'home',
                meta: {title: '首页'},
            },
            {
                path: '/studentInfo',
                component: Profile,
                name: 'studentInfo',
                meta: {title: '个人信息管理'},
            },
            {
                path: '/researchInfo',
                component: StudentResearchInfo,
                name: 'researchInfo',
                meta: {title: '统考信息查看'},
            },
            {
                path: '/testEssentialsList',
                component: StudentTestEssentialsList,
                name: 'studentTestEssentialsList',
                meta: {title: '重难点信息查看'},
            },
            {
                path: '/question/list',
                component: StudentQuestionIndex,
                name: 'studentExaminationQuestionList',
                meta: {title: '题目练习'},
                children: [
                    {
                        path: '/question/list',
                        component: StudentExaminationQuestionList,
                        name: 'studentExaminationQuestionList',
                        meta: { title: '题目列表' }
                    },
                    {
                        path: '/question/answer',
                        component: StudentAnswerQuestion,
                        name: 'studentAnswerQuestion',
                        meta: { title: '练习' }
                    }
                ]
            },
            {
                path: '/record/list',
                component: StudentAnswerRecordIndex,
                name: 'studentAnswerRecordIndex',
                meta: {title: '答题记录管理'},
                children: [
                    {
                        path: '/record/list',
                        component: StudentAnswerRecordList,
                        name: 'studentAnswerRecordList',
                        meta: { title: '做题记录' }
                    },
                    {
                        path: '/record/err',
                        component: StudentErrorQuestionList,
                        name: 'studentErrorQuestionList',
                        meta: { title: '错题列表' }
                    }
                ]
            },
        ]
    },
    //添加教师端模块路由
    {
        path: '/teacher',
        component: TeacherLayout,
        meta: { requiresAdmin: true },
        redirect: '/teacher/student',
        children: [
            {
                path: '/teacher/student',
                name: 'TeacherStudentIndex',
                component: TeacherStudentIndex,
                meta: {title: '学生管理'},
                children: [
                    {
                        path: '/teacher/student',
                        component: TeacherStudentList,
                        name: 'TeacherStudentList',
                        meta: { title: '学生列表' }
                    }
                ]
            },
            {
                path: '/teacher/self',
                name: 'TeacherSelfIndex',
                component: TeacherSelfIndex,
                meta: {title: '教师信息管理'},
                children: [
                    {
                        path: '/teacher/self',
                        component: TeacherSelfInfoIndex,
                        name: 'TeacherSelfInfoIndex',
                        meta: { title: '个人信息' }
                    }
                ]
            },
            {
                path: '/teacher/researchInfo',
                name: 'ResearchInfoIndex',
                component: ResearchInfoIndex,
                meta: {title: '考研信息管理'},
                children: [
                    {
                        path: '/teacher/researchInfo',
                        component: ResearchInfoList,
                        name: 'ResearchInfoList',
                        meta: { title: '信息列表' }
                    }
                ]
            },
            {
                path: '/teacher/examinationQuestion',
                name: 'ExaminationQuestionIndex',
                component: ExaminationQuestionIndex,
                meta: {title: '统考题目管理'},
                children: [
                    {
                        path: '/teacher/examinationQuestion',
                        component: ExaminationQuestionList,
                        name: 'ExaminationQuestionList',
                        meta: { title: '题目列表' }
                    }
                ]
            },
            {
                path: '/teacher/testEssentials',
                name: 'TestEssentialsIndex',
                component: TestEssentialsIndex,
                meta: {title: '重难点管理'},
                children: [
                    {
                        path: '/teacher/testEssentials',
                        component: TestEssentialsList,
                        name: 'testEssentials',
                        meta: { title: '重难点列表' }
                    }
                ]
            },
            {
                path: '/teacher/answerDataTest',
                name: 'answerDataIndex',
                component: AnswerDataIndex,
                meta: {title: '学生答题数据管理'},
                children: [
                    {
                        path: '/teacher/answerDataTest',
                        component: AnswerData,
                        name: 'answerData',
                        meta: { title: '练习题' }
                    }
                ]
            },
            {
                path: '/teacher/answerRecord',
                name: 'answerRecordIndex',
                component: AnswerRecordIndex,
                meta: {title: '题目批改管理'},
                children: [
                    {
                        path: '/teacher/answerRecord',
                        component: AnswerRecordList,
                        name: 'answerRecordList',
                        meta: { title: '批改列表' }
                    }
                ]
            }
        ]
    },
    //添加管理员端模块路由
    {
        path: '/admin',
        component: AdminLayout,
        meta: { requiresAdmin: true },
        redirect: '/admin/student',
        children: [
            {
                path: '/admin/student',
                name: 'AdminStudentIndex',
                component: AdminStudentIndex,
                meta: {title: '学生管理'},
                children: [
                    {
                        path: '/admin/student',
                        component: AdminStudentManageList,
                        name: 'AdminStudentManageList',
                        meta: { title: '学生列表' }
                    }
                ]
            },
            {
                path: '/admin/teacher',
                name: 'AdminTeacherIndex',
                component: AdminTeacherIndex,
                meta: {title: '学生管理'},
                children: [
                    {
                        path: '/admin/teacher',
                        component: AdminTeacherManageList,
                        name: 'AdminTeacherManageList',
                        meta: { title: '学生列表' }
                    }
                ]
            }
        ]
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes,
});

// 添加全局路由守卫进行权限验证
router.beforeEach((to, from, next) => {
    const userRole = localStorage.getItem('userRole')

    // 不需要验证的路由（登录、注册等）
    const publicPages = ['/login', '/registry', '/backPasswordNew']
    if (publicPages.includes(to.path)) {
        return next()
    }

    console.log(to.path)
    console.log(userRole)

    // 根据不同角色进行路由控制
    if (to.path.startsWith('/admin')) {
        if (userRole === '1') {
            next()
        } else {
            ElMessage.error('无权访问管理员页面')
            next('/home')
        }
    } else if (to.path.startsWith('/teacher')) {
        if (userRole === '2') {
            next()
        } else {
            console.log(to)
            ElMessage.error('无权访问教师页面')
            next('/home')
        }
    } else if (to.path.startsWith('/home') || to.path.startsWith('/studentInfo')) {
        if (userRole === '0') {
            next()
        } else if (userRole === '1') {
            next('/admin/student')
        } else if (userRole === '2') {
            next('/teacher/student')
        }
    } else {
        next()
    }
})

export default router;
